﻿// JScript 文件

Ext.onReady(function () {
    // 使用表单提示
    Ext.QuickTips.init();
    // turn on validation errors beside the field globally
    Ext.form.Field.prototype.msgTarget = 'side';

    var sm = new Ext.grid.CheckboxSelectionModel();

    var cm = new Ext.grid.ColumnModel([
	    new Ext.grid.RowNumberer(),
		sm, 
		{ header: '名称', width: 200, align: 'left', dataIndex: 'proName' },
		{ header: '编码', width: 80, align: 'left', dataIndex: 'proNO' } ,
		{ header: '性别', width: 50, align: 'left', dataIndex: 'sex' } ,
		{ header: '款式', width: 100, align: 'left', dataIndex: 'style' } ,
		{ header: '材质', width: 100, align: 'left', dataIndex: 'material' } ,
		{ header: '成本', width: 50, align: 'left', dataIndex: 'costPrice' } ,
		{ header: '全价', width: 50, align: 'left', dataIndex: 'wholeSalePrice' } ,
		{ header: '折后价', width: 50, align: 'left', dataIndex: 'retailPrice' } ,
		{ header: '单位', width: 50, align: 'left', dataIndex: 'unit' }	,
		{ header: '服装特点', width: 260, align: 'left', dataIndex: 'especial' } 
		
  
	]);

    cm.defaultSortable = true;

    var ds = new Ext.data.GroupingStore({
        proxy: new Ext.data.HttpProxy({
            url: 'ashx/Product_GetByPage.ashx'
        }),
        reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
        },
		[{ name: 'id', mapping: 'proID', type: 'string' }, 
		 { name: 'proName', type: 'string' },
         { name: 'proNO', type: 'string' },
         { name: 'sex', type: 'string' },
         { name: 'style', type: 'string' },
         { name: 'material', type: 'string' },
         { name: 'costPrice', type: 'string' },
         { name: 'wholeSalePrice', type: 'string' },
         { name: 'retailPrice', type: 'string' },
         { name: 'unit', type: 'string' },
         { name: 'especial', type: 'string' }
		])
    });
    
    var searchbar = new Ext.Toolbar({
        id: 'searchbar',
        items: ['名称  ： ',
		    new Ext.form.TextField({
		        id: 'proName',
		        width: 100
		    }), ' ',
		    '编码  ： ',
		    new Ext.form.TextField({
		        id: 'proNO',
		        width: 100
		    }), ' ',
			new Ext.Toolbar.Button({
			    id: 'searchbutton',
			    iconCls: 'icon_search',
			    text: '查询',
			    handler: doSearch
			})
		]
    });

    function doSearch() {
        ds.load(
	        { params: {
	            start: 0,
	            limit: 20,
	            proNO: Ext.get('proNO').dom.value,
	            proName: Ext.get('proName').dom.value
	        }
	        });
    };
    
    var paging = new Ext.PagingToolbar({
        pageSize: 100,
        store: ds,
        displayInfo: true,
        displayMsg: '显示第{0}条到{1}条记录，一共{2}条',
        items:
		["-", " ",
		    { text: '添加',
		        tooltip: '添加一件商品',
		        iconCls: 'icon-add',
		        handler: function () {
		            addNewBrand();
		        }
		    },
		"-", " ",
		    { text: '编辑详细信息',
		        tooltip: '修改商品信息',
		        iconCls: 'icon-edit',
		        handler: function () {
		            var _record = grid.getSelectionModel().getSelected();
		            if (sm.getCount() == 1) {
		                window.location.href = "productAdd.aspx?pid=" + _record.data.id;
		            } else
		                Ext.Msg.alert('提示', '请选择一条记录！');

		        }
		    },
		"-", " ", { text: '删除',
		    tooltip: '删除选择的衣服',
		    iconCls: 'icon-del',
		    handler: function () {

		        var _record = grid.getSelectionModel().getSelected();
		        if (sm.getCount() == 1) {
		            del_brand(_record);
		        } else
		            Ext.Msg.alert('提示', '请选择一条记录！');

		    }
		}
		],
        emptyMsg: "查询记录为空!"
    });


 //add-win
    function addNewBrand() {
        var window_add_product = new Ext.Window({
            title: '添加衣服',
            width: 440,
            height: 280,
            resizable: false,
            autoHeight: true,
            modal: true,
            closeAction: 'hide',
            listeners: {
                'hide': function () {
                    this.setTitle('添加衣服');
                }
            },
            items: [{
                xtype: "form",
                name:"AddForm",
                labelWidth: 70,
                labelAlign: 'right',
                url: 'ashx/Product_Add.ashx',
                border: false,
                baseCls: 'x-plain',
                bodyStyle: 'padding:5px 5px 0',
                anchor: '100%',
                defaults: {
                    width: 417,
                    msgTarget: 'side'
                },
                items: [{
                    xtype: "fieldset",
                    title: '',
                    autoHeight: true,
                    items: [ 
                     {
                        xtype: "textfield",
                        width: 300,
                        fieldLabel: "名称",
                        name: "product.proName",
                        //regex: /^\S+$/,
                        regexText: "请输入衣服名!",
                        allowBlank: false,
                        blankText: '该字段不允许为空',
                        msgTarget: 'side',
                        maxLength: 200
                    },   {   
                        xtype: "textfield",
                        width: 300,
                        fieldLabel: "编码",
                        name: "product.proNO",
                        regex: /^\S+$/,
                        regexText: "请输入衣服名!",
                        allowBlank: false,
                        blankText: '该字段不允许为空',
                        msgTarget: 'side',
                        maxLength: 10
                    } , {
                        xtype: "textfield",
                        width: 300,
                        fieldLabel: "款式",
                        name: "product.style",
                        allowBlank: true,
                        msgTarget: 'side',
                        maxLength: 50
                    },{
                        xtype: "textfield",
                        width: 300,
                        fieldLabel: " 材质",
                        name: "product.mateiral",
                        allowBlank: true,
                        msgTarget: 'side',
                        maxLength: 50
                    },{
                        xtype: "textfield",
                        width: 300,
                        fieldLabel: "成本",
                        name: "product.costPrice",
                        allowBlank: true,
                        msgTarget: 'side',
                        maxLength: 5
                    },{
                        xtype: "textfield",
                        width: 300,
                        fieldLabel: "全价",
                        name: "product.wholeSalePrice",
                        allowBlank: true,
                        msgTarget: 'side',
                        maxLength: 5
                    },{
                        xtype: "textfield",
                        width: 300,
                        fieldLabel: "折后价",
                        name: "product.retailPrice",
                        allowBlank: true,
                        msgTarget: 'side',
                        maxLength: 5
                    },{
                        xtype: "textfield",
                        width: 300,
                        fieldLabel: "特点",
                        name: "product.especial",
                        allowBlank: true,
                        msgTarget: 'side',
                        maxLength: 50
                    },{
                        xtype: "combo",
                        width: 300,
                        fieldLabel: "单位",
                        name: "product.unit",
                        allowBlank: true,
                        msgTarget: 'side',
                        maxLength: 50,
                        store: new Ext.data.SimpleStore({
                            fields: ["value", "text"],
                            data: [['件', '件'], ['套', '套']] 
                        }),
                        displayField: 'text',
                        valueField: 'value',
                        mode: 'local',
                        emptyText: '请选择状态',
                        allowBlank: false,
                        blankText: '该字段不允许为空',
                        msgTarget: 'side',
                        editable: false,
                        triggerAction: 'all',
                        listWidth: 200
                    }]
                }],
                buttonAlign: 'center',
                minButtonWidth: 60,
                buttons: [{
                    text: '添加',
                    handler: function (btn) {
                        var frm = this.ownerCt.ownerCt.form;
                        if (frm.isValid()) {
                            btn.disable();
                            var cnfield = frm.findField('product.proName');
                            frm.submit({
                                waitTitle: '请稍候',
                                waitMsg: '正在提交表单数据,请稍候...',
                                success: function (form, action) {
                                    alert("添加成功！请填写详细信息！");
                                    window.location.href = "productAdd.aspx?pid=" + action.result.msg;
                                    
                                     
//                                    Ext.Msg.show({
//                                        title: '成功提示',
//                                        msg: '添加成功!',
//                                        buttons: Ext.Msg.OK,
//                                        fn: function () {
//                                            cnfield.focus(true);
//                                            btn.enable();
//                                        },
//                                        icon: Ext.Msg.INFO
//                                    });
//                                    cnfield.reset();
//                                    window_add_product.hide();
//                                    ds.load();
                                },
                                failure: function () {
                                    Ext.Msg.show({
                                        title: '错误提示',
                                        msg: '添加失败，请联系管理员!',
                                        buttons: Ext.Msg.OK,
                                        fn: function () {
                                            btn.enable();
                                        },
                                        icon: Ext.Msg.ERROR
                                    });
                                }
                            });
                        }
                    }
                }, {
                    text: '重置',
                    handler: function () {
                        this.ownerCt.ownerCt.form.reset();
                    }
                }, {
                    text: '取消',
                    handler: function () {
                        window_add_product.hide();
                    }
                }]
            }]
        });

        window_add_product.show();    
    }
    
    var grid = new Ext.grid.EditorGridPanel({
        renderTo: 'grid-div',
        cm: cm,
        ds: ds,
        sm: sm, 
        tbar: searchbar,
        bbar: paging,
        height: 500,
        border: true,
        bodyStyle: 'width:1020px',
        stripeRows: true,
        title: '商品管理'
    });

    function del_brand(record) { 
        var myMask = new Ext.LoadMask(Ext.getBody(), {
            msg: "Please wait..."
        });
        Ext.Ajax.request({
            url: 'ashx/Product_Del.ashx',
            params:{
                 pid:record.data.id
            },
            success: function (response) {
                ds.load();
                myMask.hide();
            },
            failure: function () {

                Ext.Msg.alert('提示', '操作失败！');
                myMask.hide();
            }
        });
    } 
    ds.load();

});